package org.example.dao;

import org.apache.ibatis.annotations.*;
import org.example.domain.User;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author Jimmy
 * @version 1.0
 */


public interface UserDao {

    /**
     * 根据邮箱和密码查询并返回user
     * @param email 邮箱
     * @param password 密码
     * @return user
     */
    @Select("select user_id id,user_name username,email,telephone from ss_user where email=#{email} and password=#{password}")
    @ResultType(User.class)
    User login(@Param("email") String email, @Param("password") String password);

    /**
     * 根据id查询rid
     * @param id id
     * @return list
     */
    @Select("SELECT role_id id FROM ss_role_user WHERE user_id=#{id}")
    List<String> findRid(String id);

    /**
     * 分页查询(不含条件)
     * @param pagenum 当前页
     * @param pagesize 每页显示条目数
     * @return list
     */
//    @Select("<script>select user_id id,email,user_name username,state,gender,telephone,birthday,join_date joinDate,dept_id deptId " +
//            " from ss_user where 1=1 " +
//            "<if test= \"query != null \"> gender=#{query} </if>" +
//            "</script>")
//    @ResultType(User.class)
//    List<User> findByPage(@Param("pageNumKey")int pagenum,@Param("pageSizeKey")int pagesize);

    /**
     * 分页查询（包含条件）
     * @param user user
     * @return list
     */
    @Select("<script>select user_id id,email,user_name username,state,gender,telephone,birthday,join_date joinDate,dept_id deptId " +
            " from ss_user where 1=1 " +
            "<if test= 'username != null '> and user_name like concat(concat('%',#{username}),'%') </if>" +
            "<if test= 'email != null '> and email like concat(concat('%',#{email}),'%') </if>" +
            "<if test= 'telephone != null '> and telephone like concat(concat('%',#{telephone}),'%') </if>" +
            "</script>")
    List<User> findByPage(User user);

    /**
     * 查询总条目数
     * @return int
     */
//    @Select("select count(*) from ss_user")
//    int findTotal();

    /**
     * 查询总条目数
     * @param user user
     * @return int
     */
    @Select("<script>select count(*) " +
            "from ss_user where 1=1 " +
            "<if test= 'username != null '> and user_name like concat(concat('%',#{username}),'%') </if>" +
            "<if test= 'email != null '> and email like concat(concat('%',#{email}),'%') </if>" +
            "<if test= 'telephone != null '> and telephone like concat(concat('%',#{telephone}),'%') </if>" +
            "</script>")
    int findTotal(User user);

    /**
     * 根据id返回一个user
     * @param id id
     * @return user
     */
    @Select("select user_id id,email,user_name username,state,gender,telephone,birthday,join_date joinDate,dept_id deptId from ss_user where user_id=#{id}")
    @ResultType(User.class)
    User findById(@Param("id") String id);

    /**
     * 修改user
     * @param user user
     * @return int
     */
    @Update("<script>update ss_user" +
            "<set>" +
            "  <if test='email != null'> email=#{email}, </if>" +
            "  <if test='username != null'> user_name=#{username}, </if>" +
            "  <if test='telephone != null'> telephone=#{telephone}, </if>" +
            "  <if test='deptId != null'> dept_id=#{deptId}, </if>" +
            "  <if test='state != null'> state=#{state}, </if>" +
            "</set>" +
            " where user_id=#{id}" +
            "</script>")
    int update(User user);

    /**
     * 删除用户
     * @param id 用户ID
     * @return int
     */
    @Delete("delete from ss_user where user_id=#{id}")
    int delete(String id);
}
